/*   1:    */ package com.atomikos.persistence.imp;
/*   2:    */ 
/*   3:    */ import com.atomikos.finitestates.FSMEnterEvent;
/*   4:    */ import com.atomikos.finitestates.FSMPreEnterListener;
/*   5:    */ import com.atomikos.persistence.LogException;
/*   6:    */ import com.atomikos.persistence.ObjectImage;
/*   7:    */ import com.atomikos.persistence.ObjectLog;
/*   8:    */ import com.atomikos.persistence.StateRecoverable;
/*   9:    */ import com.atomikos.persistence.StateRecoveryManager;
/*  10:    */ import java.util.Enumeration;
/*  11:    */ import java.util.Vector;
/*  12:    */ 
/*  13:    */ public class StateRecoveryManagerImp
/*  14:    */   implements StateRecoveryManager, FSMPreEnterListener
/*  15:    */ {
/*  16:    */   protected ObjectLog objectlog_;
/*  17:    */   
/*  18:    */   public StateRecoveryManagerImp(ObjectLog objectlog)
/*  19:    */   {
/*  20: 62 */     this.objectlog_ = objectlog;
/*  21:    */   }
/*  22:    */   
/*  23:    */   public void init()
/*  24:    */     throws LogException
/*  25:    */   {
/*  26: 71 */     this.objectlog_.init();
/*  27:    */   }
/*  28:    */   
/*  29:    */   public void register(StateRecoverable staterecoverable)
/*  30:    */   {
/*  31: 80 */     if (staterecoverable == null) {
/*  32: 81 */       throw new IllegalArgumentException("null in register arg");
/*  33:    */     }
/*  34: 82 */     Object[] states = staterecoverable.getRecoverableStates();
/*  35: 83 */     if (states != null)
/*  36:    */     {
/*  37: 84 */       for (int i = 0; i < states.length; i++) {
/*  38: 85 */         staterecoverable.addFSMPreEnterListener(this, states[i]);
/*  39:    */       }
/*  40: 87 */       states = staterecoverable.getFinalStates();
/*  41: 88 */       for (int i = 0; i < states.length; i++) {
/*  42: 89 */         staterecoverable.addFSMPreEnterListener(this, states[i]);
/*  43:    */       }
/*  44:    */     }
/*  45:    */   }
/*  46:    */   
/*  47:    */   public void preEnter(FSMEnterEvent event)
/*  48:    */     throws IllegalStateException
/*  49:    */   {
/*  50:100 */     Object state = event.getState();
/*  51:101 */     StateRecoverable source = (StateRecoverable)event.getSource();
/*  52:102 */     ObjectImage img = source.getObjectImage(state);
/*  53:103 */     if (img != null)
/*  54:    */     {
/*  55:105 */       StateObjectImage simg = new StateObjectImage(img);
/*  56:106 */       Object[] finalstates = source.getFinalStates();
/*  57:107 */       boolean delete = false;
/*  58:109 */       for (int i = 0; i < finalstates.length; i++) {
/*  59:110 */         if (state.equals(finalstates[i])) {
/*  60:111 */           delete = true;
/*  61:    */         }
/*  62:    */       }
/*  63:    */       try
/*  64:    */       {
/*  65:115 */         if (!delete) {
/*  66:116 */           this.objectlog_.flush(simg);
/*  67:    */         } else {
/*  68:118 */           this.objectlog_.delete(simg.getId());
/*  69:    */         }
/*  70:    */       }
/*  71:    */       catch (LogException le)
/*  72:    */       {
/*  73:120 */         throw new IllegalStateException("could not flush state image " + le.getMessage() + " " + le.getClass().getName());
/*  74:    */       }
/*  75:    */     }
/*  76:    */   }
/*  77:    */   
/*  78:    */   public void close()
/*  79:    */     throws LogException
/*  80:    */   {
/*  81:134 */     this.objectlog_.close();
/*  82:    */   }
/*  83:    */   
/*  84:    */   public StateRecoverable recover(Object id)
/*  85:    */     throws LogException
/*  86:    */   {
/*  87:143 */     StateRecoverable srec = (StateRecoverable)this.objectlog_.recover(id);
/*  88:144 */     if (srec != null) {
/*  89:145 */       register(srec);
/*  90:    */     }
/*  91:146 */     return srec;
/*  92:    */   }
/*  93:    */   
/*  94:    */   public Vector recover()
/*  95:    */     throws LogException
/*  96:    */   {
/*  97:155 */     Vector ret = this.objectlog_.recover();
/*  98:156 */     Enumeration enumm = ret.elements();
/*  99:157 */     while (enumm.hasMoreElements())
/* 100:    */     {
/* 101:158 */       StateRecoverable srec = (StateRecoverable)enumm.nextElement();
/* 102:159 */       register(srec);
/* 103:    */     }
/* 104:161 */     return ret;
/* 105:    */   }
/* 106:    */   
/* 107:    */   public void delete(Object id)
/* 108:    */     throws LogException
/* 109:    */   {
/* 110:170 */     this.objectlog_.delete(id);
/* 111:    */   }
/* 112:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.persistence.imp.StateRecoveryManagerImp
 * JD-Core Version:    0.7.0.1
 */